﻿package com.CreatPDF;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.DBHelper.MySQLIteOpenHelper;
import com.document.MyActivity;
import com.document.R;
import com.document.Creat_By_LocalFile;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter;
//文件浏览视图类
public class browse extends MyActivity {
	private  ListView listV = null;
	private List<File> list = null;
	private int a[] = {R.drawable.img,R.drawable.file};
	private ArrayList<HashMap<String, Object>> recordItem;
	
	private File presentFile;
	static String nameStr ;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.browser);
		listV = (ListView)findViewById(R.id.list_1);
		list_files();
	}
	private void list_files(){
		File path = android.os.Environment.getExternalStorageDirectory();
		presentFile = path;
		File[] file = path.listFiles();
		fill(file);
	}
	private void fill(File[] file){
		SimpleAdapter adapter = null;
		recordItem = new ArrayList<HashMap<String, Object>>();
		list = new ArrayList<File>();
		for(File f: file){
			if(Invalid(f) == 1){
				list.add(f);
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("picture", a[0]);
				map.put("name", f.getName());
				recordItem.add(map);
			}
			if(Invalid(f) == 0){
				list.add(f);
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("picture", a[1]);
				map.put("name", f.getName());
				recordItem.add(map);
			}
		}
		
		
		adapter = new SimpleAdapter(this, recordItem, R.layout.browseritem, new String[]{"picture", "name"}, new int[]{R.id.browserItemIcon, R.id.browserItemText});
		listV.setAdapter(adapter);
		listV.setAdapter(adapter);
		listV.setOnItemClickListener(new Clicker());
	}

	private int Invalid(File f){
		if(f.isDirectory()){
			return 0;
		}
		else{
			//取得文件后缀，选择显示需要的文件
			String filename = f.getName().toLowerCase();
			if(filename.endsWith(".png")||filename.endsWith(".jpg")||filename.endsWith(".WMF")||filename.endsWith(".EMF")||filename.endsWith(".pict")){
				return 1;
			}
			return 2;
		}
	}
	//为ListView没一项设置监听,选择本地图片创建PDF
	private class Clicker implements OnItemClickListener{
		
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			Intent i = new Intent();
			
			i.setClass(browse.this, Creat_By_LocalFile.class);
			
			File file = list.get(arg2);
			presentFile = file;
			if(file.isDirectory()){
				File[] files = file.listFiles();
				fill(files);
				
			}
			else{
				nameStr = file.getAbsolutePath();
				MySQLIteOpenHelper helper=new MySQLIteOpenHelper(getApplicationContext());
				String insertsql="insert into FileName values('"+nameStr+"')";
				helper.insert(insertsql);
				helper.close();
				
				
				startActivity(i);
				
				finish();
			}
		}
	}
	// 得到文件路径 全局变量 给其他类调用
	public static String getfilepath(){
		return nameStr;
		
	}

	
}
